# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

# this aport only includes glib/gtk support in order to break the
# circular make dependency: gtk <- cups <- poppler <- gtk

pkgname=poppler-qt5
_pkgname=poppler
pkgver=24.02.0
pkgrel=1
pkgdesc="PDF rendering library based on xpdf 3.0"
url="https://poppler.freedesktop.org/"
arch="all"
license="GPL-2.0-only"
replaces="poppler-glib"
provides="poppler-qt4=$pkgver-r$pkgrel"
options="!check" # Requires dl of testfiles and only checks qt5 libs
depends_dev="
	boost-dev
	cairo-dev
	fontconfig-dev
	jpeg-dev
	lcms2-dev
	libjpeg-turbo-dev
	libpng-dev
	libxml2-dev
	openjpeg-dev
	poppler-dev>=$pkgver
	tiff-dev
	zlib-dev
	"
makedepends="
	$depends_dev
	cmake
	qt5-qtbase-dev
	qt6-qtbase-dev
	samurai
	"
subpackages="poppler-qt6:_qt6 $pkgname-dev"
source="https://poppler.freedesktop.org/poppler-$pkgver.tar.xz"
builddir="$srcdir/$_pkgname-$pkgver"

prepare() {
	default_prepare

	local _linked_pkg=poppler
	local _linked_apkbuild="$startdir"/../../main/$_linked_pkg/APKBUILD
	mkdir -p "$builddir"
	if  [ -f "$_linked_apkbuild" ]; then
		local _linked_ver=$( . "$_linked_apkbuild"; echo "$pkgver")
		if [ "$_linked_ver" != "$pkgver" ]; then
			error "pkgver needs to be identical with $_linked_pkg ($_linked_ver)"
			return 1
		fi
	fi
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DBUILD_CPP_TESTS="$(want_check && echo ON || echo OFF)" \
		-DBUILD_GTK_TESTS="$(want_check && echo ON || echo OFF)" \
		-DBUILD_MANUAL_TESTS="$(want_check && echo ON || echo OFF)" \
		-DBUILD_QT5_TESTS="$(want_check && echo ON || echo OFF)" \
		-DBUILD_QT6_TESTS="$(want_check && echo ON || echo OFF)" \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DENABLE_BOOST=ON \
		-DENABLE_GPGME=OFF \
		-DENABLE_LIBCURL=OFF \
		-DENABLE_NSS3=OFF \
		-DENABLE_QT5=ON \
		-DENABLE_QT6=ON \
		-DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
		$CMAKE_CROSSOPTS
	cmake --build build
}

package() {
	install -Dm644 build/poppler-qt5.pc -t "$pkgdir"/usr/lib/pkgconfig/
	install -Dm644 build/poppler-qt6.pc -t "$pkgdir"/usr/lib/pkgconfig/
	DESTDIR="$pkgdir" cmake --install build/qt5
	DESTDIR="$pkgdir" cmake --install build/qt6
}

_qt6() {
	pkgdesc="$pkgdesc (qt6 component)"
	amove usr/lib/libpoppler-qt6.so.*
}

sha512sums="
95a208d21ac4d2d308a7ab3da43b95092ef78cd55ebe873c97e0d6c12d8b9d5c4614f83087616c35e1ed9d67ca606a5e008a98698bd12a332a8206ed4cf55500  poppler-24.02.0.tar.xz
"
